/*******************************************************************************
 * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
 * @file     popupBox.c
 * @author   UartTFT Application Team
 * @version  V0.0.1
 * @date     2023-01-11
 * @brief    This file provides UartTFT Application display function
 ********************************************************************************/
#include "popupBox.h"
#include "bsp.h"
#include "levetop.h"

extern void Display_Icon(uint16_t id, uint16_t x, uint16_t y, uint8_t Display_mode);
extern void touch_buzzer(void);
extern void LT_SendData_CRC_Frame(uint8_t *buf, uint8_t len);

/*pop-up window*/
uint8_t Pop_up_count = 0;
uint8_t Pop_up_flag = 0;
uint8_t Pop_up_num = 0;
uint8_t Pop_up_press = 0;
uint16_t Pop_up_x = 0;
uint16_t Pop_up_y = 0;
uint16_t Pop_up_id = 0;
Pop_up_Info gPop_up_Info[POPUP_SIZE];
/***********************************************************************************
 * Function Name : pop_up_touch
 * Description   : Touch detection of pop-up window.
 * Input         : None
 * Output        : None
 * Return        : None
 ************************************************************************************/
void pop_up_touch(void)
{
	uint8_t i = 0;
	uint8_t rebuf[20];
	uint16_t w, h;

	// press down
	if (gTpInfo.sta)
	{
		for (i = 0; i < Pop_up_count; i++)
		{
			if (gTpInfo.x[0] >= gPop_up_Info[i].Xs && gTpInfo.y[0] >= gPop_up_Info[i].Ys &&
				gTpInfo.x[0] <= gPop_up_Info[i].Xe && gTpInfo.y[0] <= gPop_up_Info[i].Ye &&
				Pop_up_press == 0 && button_Press_flag == 0)
			{
				if (gPop_up_Info[i].press_id != 0xFFFF)
				{
					Display_Icon(gPop_up_Info[i].press_id, gPop_up_Info[i].Xs, gPop_up_Info[i].Ys, 0);
				}

				if (Pop_up_flag == 0)
				{
					if ((gPop_up_Info[i].function & 0x01) == 1)
					{
					}
					else
					{
					}
				}
				Pop_up_num = i;
				button_Press_flag = 1;
				Pop_up_press = 1;
				// Pop_up_flag = 1;
				if (buzzer)
					touch_buzzer();
			}
		}
	}

	if (Button_Key_Code_Flag == 1)
	{
		for (i = 0; i < Pop_up_count; i++)
		{
			if (gPop_up_Info[i].Xs == 0xFFFF && Button_Key_Code == gPop_up_Info[i].Ys)
			{

				Button_Key_Code_Flag = 0;
				Button_Key_Code = 0;
				if (Pop_up_flag == 0)
				{
					// reserve
					if ((gPop_up_Info[i].function & 0x01) == 1)
					{
					}
					else
					{
					}
				}
				Pop_up_num = i;
				button_Press_flag = 1;
				Pop_up_press = 1;
				// Pop_up_flag = 1;
				break;
			}
		}
	}

	if (gTpInfo.sta == 0 && Pop_up_press == 1)
	{
		if (gPop_up_Info[Pop_up_num].Code == 0xC008)
		{
			rebuf[3] = 0x41;
			rebuf[4] = 0xFF;
			rebuf[5] = 0xFF;
			rebuf[6] = gPop_up_Info[Pop_up_num].Keyvalue >> 8;
			rebuf[7] = gPop_up_Info[Pop_up_num].Keyvalue & 0xFF;
			LT_SendData_CRC_Frame(rebuf, 5);
		}

		Pop_up_flag = 0;
		if (gPop_up_Info[Pop_up_num].id != 0xFFFF)
		{
			Display_Icon(gPop_up_Info[Pop_up_num].id, gPop_up_Info[Pop_up_num].Xs, gPop_up_Info[Pop_up_num].Ys, 0);
		}
		Pop_up_flag = 1;

		if (gPop_up_Info[Pop_up_num].function & 0x02)
		{
		}

		Pop_up_x = gPop_up_Info[Pop_up_num].pop_up_x;
		Pop_up_y = gPop_up_Info[Pop_up_num].pop_up_y;
		Pop_up_id = gPop_up_Info[Pop_up_num].pop_up_id;

		if (gPop_up_Info[Pop_up_num].pop_up_id != 0xFFFF)
		{
			Display_pop_up(gPop_up_Info[Pop_up_num].pop_up_id, gPop_up_Info[Pop_up_num].pop_up_x, gPop_up_Info[Pop_up_num].pop_up_y);
		}

		Pop_up_press = 0;
		button_Press_flag = 0;
	}
}
